package org.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author yexi
 */
public class RabbitMQUtil {

    public static final String RABBITMQ_HOST;

    public static final int RABBITMQ_PORT;

    public static final String RABBITMQ_USERNAME;

    public static final String RABBITMQ_PASSWORD;

    public static final String RABBITMQ_VIRTUAL_HOST;

    static {
        RABBITMQ_HOST = "localhost";
        RABBITMQ_PORT = 5672;
        RABBITMQ_USERNAME = "guest";
        RABBITMQ_PASSWORD = "guest";
        RABBITMQ_VIRTUAL_HOST = "/";
    }

    /**
     * 构建RabbitMQ的连接对象
     */
    public static Connection getConnection() throws Exception {
        // 创建Connection工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置RabbitMQ的连接信息
        factory.setHost(RABBITMQ_HOST);
        factory.setPort(RABBITMQ_PORT);
        factory.setUsername(RABBITMQ_USERNAME);
        factory.setPassword(RABBITMQ_PASSWORD);
        factory.setVirtualHost(RABBITMQ_VIRTUAL_HOST);

        // 返回连接对象
        Connection connection = factory.newConnection();
        return connection;
    }

}